package com.samphanie.auiu.admin.controller;

import com.samphanie.auiu.admin.feign.IAuiuAuthService;
import com.samphanie.auiu.common.dto.CreateUserDto;
import com.samphanie.auiu.common.dto.UpdateUserDto;
import com.samphanie.auiu.common.utils.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Api(tags = "后台用户管理")
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class UserAdminController {

    private final IAuiuAuthService authService;

    @ApiOperation("查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "显示条数", paramType = "query",  dataTypeClass = Integer.class),
    })
    @GetMapping("/user/list")
    public ApiResponse<?> findAllUsers(@ApiIgnore @RequestParam Map<String, Object> parameters) {
        return authService.findAllUsers(parameters);
    }

    @ApiOperation("根据用户名查询用户")
    @GetMapping("/users/{username}")
    public ApiResponse<?> findByUsername(@PathVariable String username) {
        return authService.findByUsername(username);
    }

    @ApiOperation("新增用户")
    @PostMapping("/user/add")
    public ApiResponse<?> createUser(@RequestBody CreateUserDto createUser) {
        return authService.createUser(createUser);
    }

    @ApiOperation("编辑用户")
    @PutMapping("/user/update")
    public ApiResponse<?> update(@Valid @RequestBody UpdateUserDto userDto) {
        return authService.updateUser(userDto);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/user/delete")
    public ApiResponse<?> delete(@RequestBody Long[] ids) {
        return authService.deleteUser(ids);
    }

    @ApiOperation("查询指定用户的可分配角色列表")
    @ApiImplicitParam(name = "username", value = "用户账户", paramType = "path", dataTypeClass = String.class, required = true)
    @GetMapping("/{username}/roles/available")
    public ApiResponse<?> getUserAvailableRoles(@PathVariable String username) {
        return authService.getUserAvailableRoles(username);
    }

    @ApiOperation("校验用户名")
    @ApiImplicitParam(name = "username", value = "用户名称", paramType = "query",  dataTypeClass = String.class, required = true)
    @GetMapping("/user/checkUsername")
    public ApiResponse<?> checkUsername(@RequestParam String username) {
        return authService.checkUsername(username);
    }

    @ApiOperation("校验邮箱")
    @ApiImplicitParam(name = "email", value = "邮箱号", paramType = "query",  dataTypeClass = String.class, required = true)
    @GetMapping("/user/checkEmail")
    public ApiResponse<?> checkEmail(@RequestParam String email) {
        return authService.checkEmail(email);
    }

    @ApiOperation("校验手机号")
    @ApiImplicitParam(name = "mobile", value = "手机号", paramType = "query",  dataTypeClass = String.class, required = true)
    @GetMapping("/user/checkMobile")
    public ApiResponse<?> checkMobile(@RequestParam String mobile) {
        return authService.checkMobile(mobile);
    }
}
